/*
 * PaymentTypeSelectionDialog.java
 *
 * Created on August 25, 2006, 3:47 PM
 */
package com.floreantpos.ui.dialog;

import com.floreantpos.POSConstants;
import com.floreantpos.main.Application;
import com.floreantpos.model.Guest;
import com.floreantpos.model.dao.GuestDAO;
import com.floreantpos.ui.views.CardPaymentView;
import com.floreantpos.ui.views.PaymentView;
import com.floreantpos.ui.views.payment.CashPaymentView;
import com.floreantpos.ui.views.payment.GiftCertificatePaymentView;
import javax.swing.JOptionPane;

/**
 *
 * @author MShahriar
 */
public class PaymentTypeSelectionDialog extends POSDialog {

  private PaymentView selectedPaymentView;
  private Boolean giftCertStatus;
  private Boolean btnCancelEnabled;
  private String taxId;
  private int guestID;

  public PaymentTypeSelectionDialog(String inputTaxId) {
    super(Application.getPosWindow(), true);
    initComponents();
    guestID = 0;
    taxId = inputTaxId;
    giftCertStatus = true;
    btnCancelEnabled = true;
    btnGiftCert.setVisible(giftCertStatus);
    btnCancel.setEnabled(btnCancelEnabled);
    btnBankCheck.setVisible(false);   
  }

  public PaymentTypeSelectionDialog(Boolean giftCertEnabled, int registeredGuest, String inputTaxId) {
    super(Application.getPosWindow(), true);
    initComponents();

    guestID = registeredGuest;
    taxId = inputTaxId;
    giftCertStatus = giftCertEnabled;
    btnCancelEnabled = true;
    btnGiftCert.setVisible(giftCertStatus);
    btnCancel.setEnabled(btnCancelEnabled);
    btnBankCheck.setVisible(false);   
  }
  
  public PaymentTypeSelectionDialog(Boolean cancelEnabled, String inputTaxId) {
    super(Application.getPosWindow(), true);
    initComponents();

    guestID = 0;
    taxId = inputTaxId;
    giftCertStatus = true;
    btnCancelEnabled = cancelEnabled;
    btnGiftCert.setVisible(giftCertStatus);
    btnCancel.setEnabled(btnCancelEnabled);
    btnBankCheck.setVisible(false);
  }

  private void initComponents() {
    titlePanel1 = new com.floreantpos.ui.TitlePanel();
    transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel4 = new com.floreantpos.swing.TransparentPanel();
    btnCancel = new com.floreantpos.swing.PosButton();
    jSeparator1 = new javax.swing.JSeparator();
    transparentPanel3 = new com.floreantpos.swing.TransparentPanel();
    btnCredit = new com.floreantpos.swing.PosButton();
    btnCash = new com.floreantpos.swing.PosButton();
    btnGiftCert = new com.floreantpos.swing.PosButton();
    btnDebit = new com.floreantpos.swing.PosButton();
    btnBankCheck = new com.floreantpos.swing.PosButton();
    btnBenefit = new com.floreantpos.swing.PosButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    setResizable(false);
    titlePanel1.setTitle(com.floreantpos.POSConstants.SELECT_PAYMENT_TYPE);
    getContentPane().add(titlePanel1, java.awt.BorderLayout.NORTH);

    transparentPanel1.setLayout(new java.awt.BorderLayout(5, 5));

    transparentPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
    transparentPanel1.setOpaque(true);
    transparentPanel2.setLayout(new java.awt.BorderLayout(5, 5));

    transparentPanel2.setPreferredSize(new java.awt.Dimension(100, 60));
    transparentPanel4.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

    btnCancel.setText(com.floreantpos.POSConstants.CANCEL);
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCancelActionPerformed(evt);
      }
    });

    transparentPanel4.add(btnCancel);

    transparentPanel2.add(transparentPanel4, java.awt.BorderLayout.CENTER);

    transparentPanel2.add(jSeparator1, java.awt.BorderLayout.NORTH);

    transparentPanel1.add(transparentPanel2, java.awt.BorderLayout.SOUTH);

    transparentPanel3.setLayout(new java.awt.GridLayout(0, 1, 5, 5));

    btnCash.setText(com.floreantpos.POSConstants.CASH_TRANSACTION);
    btnCash.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCashActionPerformed(evt);
      }
    });
    transparentPanel3.add(btnCash);

    btnDebit.setText(com.floreantpos.POSConstants.DEBIT_CARD_TRANSACTION);
    btnDebit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnDebitActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnDebit);

    btnCredit.setText(com.floreantpos.POSConstants.CREDIT_CARD_TRANSACTION);
    btnCredit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCreditActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnCredit);

    btnBenefit.setText(com.floreantpos.POSConstants.CARD_BENEFITS);
    btnBenefit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnBenefitActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnBenefit);

    btnGiftCert.setText(com.floreantpos.POSConstants.GIFT_CERTIFICATE);
    btnGiftCert.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnGiftCertActionPerformed();
      }
    });

    transparentPanel3.add(btnGiftCert);

    btnBankCheck.setText(com.floreantpos.POSConstants.BANK_CHECK);
    btnBankCheck.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnBankCheckActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnBankCheck);

    transparentPanel1.add(transparentPanel3, java.awt.BorderLayout.CENTER);

    getContentPane().add(transparentPanel1, java.awt.BorderLayout.CENTER);

    pack();
  }

  private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
    selectedPaymentView = null;
    canceled = true;
    dispose();
  }

  private void btnBankCheckActionPerformed(java.awt.event.ActionEvent evt) {

  }

  private void btnDebitActionPerformed(java.awt.event.ActionEvent evt) {
    CardPaymentView paymentView = null;
    if (guestID != 0 || !giftCertStatus) {
      paymentView = new CardPaymentView(giftCertStatus, guestID, taxId);
    } else {
      paymentView = new CardPaymentView(taxId);
    }
    paymentView.setCardType(CardPaymentView.CARD_TYPE_DEBIT);
    selectedPaymentView = paymentView;
    dispose();
  }

  private void btnCreditActionPerformed(java.awt.event.ActionEvent evt) {
    CardPaymentView paymentView = null;
    if (guestID != 0 || !giftCertStatus) {
      paymentView = new CardPaymentView(giftCertStatus, guestID, taxId);
    } else {
      paymentView = new CardPaymentView(taxId);
    }
    paymentView.setCardType(CardPaymentView.CARD_TYPE_CREDIT);
    selectedPaymentView = paymentView;
    dispose();
  }

  private void btnBenefitActionPerformed(java.awt.event.ActionEvent evt) {
    CardPaymentView paymentView = null;
    if (guestID != 0 || !giftCertStatus) {
      paymentView = new CardPaymentView(giftCertStatus, guestID, taxId);
    } else {
      paymentView = new CardPaymentView(taxId);
    }
    paymentView.setCardType(CardPaymentView.CARD_TYPE_BENEFIT);
    selectedPaymentView = paymentView;
    dispose();
  }

  private void btnCashActionPerformed(java.awt.event.ActionEvent evt) {
    CashPaymentView paymentView = null;
    if (guestID != 0 || !giftCertStatus) {
      paymentView = new CashPaymentView(giftCertStatus, guestID, taxId);
    } else {
      paymentView = new CashPaymentView(taxId);
    }
    selectedPaymentView = paymentView;
    dispose();
  }

  private void btnGiftCertActionPerformed() {
    String guestPhone = null;

    GuestDAO dao = GuestDAO.getInstance();
    Guest registeredGuest = null;

    if (guestID != 0) {
      registeredGuest = dao.get(guestID);
      if (registeredGuest != null) {
        guestPhone = registeredGuest.getTelephone();
      }
    }

    TelephoneSelectionDialog phoneDialog = new TelephoneSelectionDialog();
    phoneDialog.setTitle(com.floreantpos.POSConstants.CUSTOMER_PHONE);

    if (guestPhone != null) {
      phoneDialog.setValue(guestPhone);
    }

    phoneDialog.pack();
    phoneDialog.open();

    if (phoneDialog.isCanceled()) {
      if (guestPhone != null) {
        return;
      } else {
        GiftCertificatePayment();
      }
    } else {
      guestPhone = phoneDialog.getValue();
      registeredGuest = dao.findGuestByTelephone(guestPhone);

      if (registeredGuest == null) {
        int option = JOptionPane.showOptionDialog(this, com.floreantpos.POSConstants.CUSTOMER_NOT_FOUND, POSConstants.CONFIRM, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"Cadastrar", "Procurar Novamente", "Cancelar"}, null);
        if (option == JOptionPane.YES_OPTION) {
          GuestDialog guestDialog = new GuestDialog(Application.getPosWindow(), true, guestPhone);
          guestDialog.open();

          registeredGuest = dao.findGuestByTelephone(guestPhone);
          if (registeredGuest == null) {
            POSMessageDialog.showError(com.floreantpos.POSConstants.ENTER_PHONE_NUMBER_AGAIN);
            btnGiftCertActionPerformed();
          }
        } else if (option == JOptionPane.NO_OPTION) {
          btnGiftCertActionPerformed();
        } else {
          return;
        }
      }
      guestID = registeredGuest.getId();
      GiftCertificatePayment(guestID);
    }
  }

  public void GiftCertificatePayment() {
    GiftCertificatePaymentView paymentView = new GiftCertificatePaymentView(taxId);
    selectedPaymentView = paymentView;
    dispose();
  }

  public void GiftCertificatePayment(int giftCertificateGuestID) {
    GiftCertificatePaymentView paymentView = new GiftCertificatePaymentView(giftCertificateGuestID, taxId);
    selectedPaymentView = paymentView;
    dispose();
  }

  public PaymentView getSelectedPaymentView() {
    return selectedPaymentView;
  }

  private com.floreantpos.swing.PosButton btnBankCheck;
  private com.floreantpos.swing.PosButton btnCancel;
  private com.floreantpos.swing.PosButton btnCash;
  private com.floreantpos.swing.PosButton btnGiftCert;
  private com.floreantpos.swing.PosButton btnCredit;
  private com.floreantpos.swing.PosButton btnDebit;
  private com.floreantpos.swing.PosButton btnBenefit;
  private javax.swing.JSeparator jSeparator1;
  private com.floreantpos.ui.TitlePanel titlePanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel2;
  private com.floreantpos.swing.TransparentPanel transparentPanel3;
  private com.floreantpos.swing.TransparentPanel transparentPanel4;
}
